<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019-07-28
 * Time: 19:39
 */
namespace app\admin\controller;
use app\admin\model\MyBuder;
use app\admin\model\MyCid;
use app\admin\model\getdatas\MySubject;
use app\admin\model\MyTable;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Editcid extends BaseController
{
    public function show_list(Request $request){
        $id = $request->param('id');
        $obj = new MySubject();
        $obj->setModelId(16);
        $model = $obj->getModel();
        $field = $obj->getField();
        $builder = Builder::table()
            ->setAction('editcid/page?cid='.$id)
            ->setModelId(16)
            ->setMonitorAction('switch','editcid/change')
            ->setMonitorAction('edit','editcid/change')
            ->setRightHeader('操作',250);
        foreach ($model['toops'] as $value){
            if($value=='alldel'){
                $builder = $builder->addToolbarDemo('alldel','全部删除',[],'editcid/del');
            }
            if($value=='back'){
//                $builder = $builder->addToolbarDemo('back','返回');
            }
        }
//        $builder = $builder->addToolbarDemo('back','返回');
        $newmodel = Db::name(getTable('column'))->where('id',$id)->find();
        $builder = $builder->addToolbarDemo('text','栏目:'.$newmodel['name']);
        foreach ($field as $value){
            if($value['show']==1){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动多选框'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name'],'editcid/change');
                }
                if($value['form_type']=='时间'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $builder = $builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $builder = $builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    $builder = $builder->setColumn('switch',$value['field'],$value['field_name'],'editcid/change',null,'center','',['开启','关闭']);
                }
                if($value['form_type']=='状态'){
                    $builder = $builder->setColumn('status',$value['field'],$value['field_name'],'editcid/change',null,'center','',['可','不可']);
                }
            }
        }
        foreach ($model['right'] as $value){
            if($value=='edit'){
                $builder->setButton($value,'编辑','editcid/show_edit');
            }
            if($value=='del'){
                $builder->setButton($value,'删除','editcid/del');
            }
//            if($value=='add'){
//                $builder->setButton($value,'添加','editcid/show_add');
//            }
            if($value=='detail'){
                $builder->setButton($value,'查看','editcid/detail');
            }
            if($value=='a'){
                $builder->setButton($value,'编辑字段','editcid/show_list');
            }
        }
        return $builder
            ->fetch();

    }
    public function page(Request $request){
        $cid = $request->param('cid');
        $limit = $request->param('limit',10);
//        return json($request->param());
        $data = Db::name(getTable('ziduancid'))
            ->where('cid',$cid)
            ->order('sort desc')
            ->select();
        $obj = new MySubject();
        $obj->setModelId(16);
        $field = $obj->getField();
        foreach ($data as $key=>$value){
            foreach ($field as $va){
                if($va['form_type']=='时间'){
                    $value[$va['field']] = date('Y-m-d',$value[$va['field']]);
                }
                if($va['form_type']=='单图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']]);
                }
                if($va['form_type']=='多图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']],true);
                }
                if($va['form_type']=='联动多选框'){
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id','in',$value[$va['field']])
                        ->select();
                    $str = '';
                    foreach ($shu as $vs){
                        $str.=','.$vs['name'];
                    }
                    $str = trim($str,',');
                    $value[$va['field']] = $str;
                }
            }
            $data[$key] = $value;
        }
        $res['data'] = $data;
        $res['total'] = count($data);
        return json_page_response($res);
    }
    public function show_add(Request $request){
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId(16);
        $field = $obj->getField();
//                echo '<pre>';
//        print_r($field);
//        exit;
        return $obj->getFormToField('editcid/add',null,true,16);
    }
    public function add(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(16);
        $data = $obj->getData($data);
//        echo '<pre>';
//        print_r($data);exit;
        $bool = MyTable::addFields($data);
//        echo $bool;exit;
        if($bool){
            MyCid::updateField($data);
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }
    public function show_edit(Request $request){
        $data = $request->param();
        $id = $request->param('id');
//        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId(16);
        $field = $obj->getField();
        $table = $obj->getModel('table');
        $datas = Db::name($table)
            ->where('id',$id)
            ->find();
//        echo '<pre>';
//        print_r($data);
//        exit;
       return  MyBuder::form()
            ->addFormAction('editcid/edit',$datas['cid'])
            ->addFormButton([
                [
                    'type'=>'submit',
                    'desc'=>'提交'
                ],
                [
                    'type'=>'back',
                    'desc'=>'返回'
                ]
            ])->addField($field,['field','type','lang','cid'])
            ->addData($datas)
            ->FormFetch();
    }
    public function edit(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(16);
        $table = $obj->getModel('table');
        $data = $obj->getData($data);
//        echo '<pre>';
//        print_r($data);
//        exit;
        if(Db::name($table)->where('id',$data['id'])->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function del(Request $request){
        $data = $request->param();
        if(Db::name(getTable('ziduancid'))->where('id','in',$data['id'])->where('delete','>',0)->delete()){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
    public function change(Request $request){
        $field = $request->param('field');
        $id = $request->param('id');
//        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId(16);
        $table = $obj->getModel('table');
//        return json($table);
        $data = Db::name($table)->where('id',$id)
            ->find();
//        return json($field);
        if($data[$field]==1){
            $data[$field]=0;
        }else{
            $data[$field]=1;
        }
//        return json($data);
        if(Db::name($table)->where('id',$id)->update($data)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }

    }
    public function detail(Request $request){
        $model_id = $request->param('model_id');
        $id = $request->param('id');
        $obj = new MySubject();
        $obj->setModelId(16);
        return $obj->getFormToField('myfield/add',$id,false,$model_id);
    }
}